/*
 * Copyright (c) 2025 EasyAdmin
 * All rights reserved.
 *
 * This file is part of the [EasAdmin] project.
 * Unauthorized copying of this file, via any medium, is strictly prohibited.
 * Proprietary and confidential.
 *
 * Author: EasyAdmin ( 1073602@qq.com )
 * Website: https://www.xhxiao.com
 */
package com.easy.app.controller.system;

import com.easy.framework.common.model.R;
import com.easy.framework.common.validate.Add;
import com.easy.framework.common.validate.Update;
import com.easy.framework.database.page.Pagination;
import com.easy.framework.poi.utils.ExcelUtils;
import com.easy.framework.web.base.BaseController;
import com.easy.module.log.annotation.Log;
import com.easy.module.log.enums.Operation;
import com.easy.module.system.entity.SysDict;
import com.easy.module.system.query.SysDictQuery;
import com.easy.module.system.service.ISysDictService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 字典类型表 控制器
 *
 * @author EasyAdmin ( 1073602@qq.com )
 * @date 2025-08-19
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dict")
public class SysDictController extends BaseController {

    private final ISysDictService sysDictService;

    @PreAuthorize("@perm.hasPermission('system:dict$list')")
    @GetMapping("/page")
    public R<Pagination<SysDict>> page(SysDictQuery query) {
        return R.success(sysDictService.page(query));
    }

    @Log(title = "字典类型", type = Operation.EXPORT)
    @PreAuthorize("@perm.hasPermission('system:dict$export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictQuery query) throws IOException {
        List<SysDict> list = sysDictService.list(query);
        ExcelUtils.exportExcel(response, list, "字典类型");
    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("@perm.hasPermission('system:dict$query')")
    @GetMapping(value = "/{dictId}")
    public R<SysDict> get(@PathVariable("dictId") Long dictId) {
        return R.success(sysDictService.getById(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@perm.hasPermission('system:dict$add')")
    @Log(title = "字典类型", type = Operation.INSERT)
    @PostMapping
    public R add(@Validated({Add.class}) @RequestBody SysDict dict) {
        if (!sysDictService.checkDictCodeUnique(dict)) {
            return R.fail("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return R.result(sysDictService.add(dict));
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("@perm.hasPermission('system:dict$edit')")
    @Log(title = "字典类型", type = Operation.UPDATE)
    @PutMapping
    public R edit(@Validated({Update.class}) @RequestBody SysDict dict) {
        if (!sysDictService.checkDictCodeUnique(dict)) {
            return R.fail("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return R.result(sysDictService.update(dict));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@perm.hasPermission('system:dict$remove')")
    @Log(title = "字典类型", type = Operation.DELETE)
    @DeleteMapping("/{dictIds}")
    public R remove(@PathVariable("dictIds") Long[] dictIds) {
        sysDictService.deleteByIds(dictIds);
        return R.success();
    }

    /**
     * 刷新字典缓存
     */
    @PreAuthorize("@perm.hasPermission('system:dict$remove')")
    @Log(title = "字典类型", type = Operation.CLEAN)
    @DeleteMapping("/refreshCache")
    public R refreshCache() {
        sysDictService.resetDictCache();
        return R.success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public R<List<SysDict>> optionselect() {
        return R.success(sysDictService.list());
    }
}
